package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.utils.UUIDUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

@Service //dubbo注解
public class ExtCproductServiceImpl implements ExtCproductService {

    @Autowired
    ExtCproductDao extCproductDao;

    @Autowired
    ContractDao contractDao;

    @Override
    public void save(ExtCproduct extCproduct) {
        //1往附件表中插入一条数据
        //a.设置id
        extCproduct.setId(UUIDUtils.getId());
        //b.计算小计
        Double amount = 0d;
        if (extCproduct.getPrice()!=null && extCproduct.getCnumber()!=null) {
            amount =extCproduct.getPrice()*extCproduct.getCnumber();
        }
        //c,设置小计
        extCproduct.setAmount(amount);
        //d.保存附件
        extCproductDao.insertSelective(extCproduct);

        //2更新合同中附件的数量和总金额
        //a.获取合同
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        //b.设置附件的样数
        contract.setExtNum(contract.getExtNum() + 1);
        //c.设置总金额
        contract.setTotalAmount(contract.getTotalAmount() + amount);
        //d.更新合同
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void update(ExtCproduct extCproduct) {
        //1 更新附件表中一条数据
        //a.获取此附件原来的金额
        Double oldAmount = extCproductDao.selectByPrimaryKey(extCproduct.getId()).getAmount();
        //b.计算小计
        Double newAmount = 0d;
        if (extCproduct.getPrice()!=null && extCproduct.getCnumber()!=null) {
            newAmount =extCproduct.getPrice()*extCproduct.getCnumber();
        }
        //c.设置小计
        extCproduct.setAmount(newAmount);
        //d.更新附件
        extCproductDao.updateByPrimaryKeySelective(extCproduct);

        //2 更新合同中总金额
        //a.获取合同
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        //b.设置总金额
        contract.setTotalAmount(contract.getTotalAmount()-oldAmount+newAmount);
        //c.更新合同
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void deleteById(String id) {
        //1.删除附件表中一条数据
        //a.获取附件和原来小计
        ExtCproduct extCproduct = extCproductDao.selectByPrimaryKey(id);
        Double amount = extCproduct.getAmount();
        //b.删除附件
        extCproductDao.deleteByPrimaryKey(id);

        //2.更新合同中附件的数量和总金额
        //a.获取合同
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        //b.设置附件的数据和总金额
        contract.setExtNum(contract.getExtNum() - 1);
        contract.setTotalAmount(contract.getTotalAmount() - amount);
        //c.更新合同
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public ExtCproduct findById(String id) {
        return extCproductDao.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(int page, int size, ExtCproductExample example) {
        PageHelper.startPage(page,size);
        return new PageInfo(extCproductDao.selectByExample(example));
    }
}
